package com.zm.demo.sc.sb.resolver;

import com.zm.demo.sc.sb.LoginUser;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author zoum
 * @create 2019/10/29 11:16
 */
public class MyArgumentResolver implements HandlerMethodArgumentResolver{

    /**
     * 判断参数是否匹配，可以依据参数类型，注解等信息匹配
     * @param methodParameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().isAssignableFrom(LoginUser.class);
    }

    /**
     * 返回匹配参数的处理结果
     * @param methodParameter
     * @param modelAndViewContainer
     * @param nativeWebRequest
     * @param webDataBinderFactory
     * @return
     * @throws Exception
     */
    @Nullable
    @Override
    public Object resolveArgument(MethodParameter methodParameter, @Nullable ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, @Nullable WebDataBinderFactory webDataBinderFactory) throws Exception {
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername("zm");
        loginUser.setPassword("zm");
        return loginUser;
    }
}
